package cn.stylefeng.guns.modular.statistic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.statistic.entity.MonthIncome;
import cn.stylefeng.guns.modular.statistic.mapper.MonthIncomeMapper;
import cn.stylefeng.guns.modular.statistic.model.params.MonthIncomeParam;
import cn.stylefeng.guns.modular.statistic.model.result.IncomeStatisticResult;
import cn.stylefeng.guns.modular.statistic.model.result.MonthIncomeResult;
import  cn.stylefeng.guns.modular.statistic.service.MonthIncomeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuqiang
 * @since 2020-02-22
 */
@Service
public class MonthIncomeServiceImpl extends ServiceImpl<MonthIncomeMapper, MonthIncome> implements MonthIncomeService {

    @Override
    public void add(MonthIncomeParam param){
        MonthIncome entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MonthIncomeParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MonthIncomeParam param){
        MonthIncome oldEntity = getOldEntity(param);
        MonthIncome newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MonthIncomeResult findBySpec(MonthIncomeParam param){
        return null;
    }

    @Override
    public List<MonthIncomeResult> findListBySpec(MonthIncomeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MonthIncomeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<IncomeStatisticResult> statisticMonth(MonthIncomeParam param) {
        List<IncomeStatisticResult> list = new ArrayList<>();

        List<MonthIncomeResult> monthStatisticResults = this.baseMapper.customList(param);
        Integer startMonth = param.getStartTime();
        Integer endMonth = param.getEndTime();
        List<Integer> rangeList = getRangeList(startMonth, endMonth);

        for(Integer month : rangeList){
            MonthIncomeResult result = monthStatisticResults.stream().filter(r -> r.getMonth().equals(month)).findFirst().orElse(null);
            if(result != null){
                list.add(new IncomeStatisticResult(month,result.getIncome()));
            }else {
                list.add(new IncomeStatisticResult(month,new BigDecimal(0.0)));
                //添加到数据库
            }
        }

        return list;
    }

    private List<Integer> getRangeList(Integer startMonth, Integer endMonth) {
        int sYear = Integer.parseInt(startMonth.toString().substring(0, 4));
        int sMonth = Integer.parseInt(startMonth.toString().substring(4, 6));
        int eYear = Integer.parseInt(endMonth.toString().substring(0, 4));
        int eMonth = Integer.parseInt(endMonth.toString().substring(4, 6));
        LocalDate startDate = LocalDate.of(sYear, sMonth,1);
        LocalDate endDate = LocalDate.of(eYear, eMonth,1);

        List<Integer> list = new ArrayList<>();
        while (endDate.compareTo(startDate) >= 0){
            list.add(Integer.parseInt(startDate.toString().replace("-","").substring(0,6)));
            startDate = startDate.plusMonths(1);
        }

        return list;
    }

    @Override
    public MonthIncomeResult findByMonth(int month) {
        return this.baseMapper.findByMonth(month);
    }

    private Serializable getKey(MonthIncomeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MonthIncome getOldEntity(MonthIncomeParam param) {
        return this.getById(getKey(param));
    }

    private MonthIncome getEntity(MonthIncomeParam param) {
        MonthIncome entity = new MonthIncome();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
